package com.wwdx.service.impl;

import com.wwdx.wechat.config.BaseWxOpenConfig;
import com.wwdx.wechat.handler.*;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author liguoliang
 * @date 2018/9/17 18:33
 */
public abstract class BaseWxOpenService extends WxOpenServiceImpl {

    private WxOpenMessageRouter wxOpenMessageRouter;
    @Resource
    private JedisPool jedisPool;

    /**
     * 第三方平台配置类
     * @return BaseWxOpenConfig 子类
     */
    protected abstract BaseWxOpenConfig getServerConfig();

    /**
     * 微信菜单处理类
     * @return AbstractMenuHandler 子类
     */
    protected abstract AbstractMenuHandler getMenuHandler();

    /**
     * 微信关注处理类
     * @return AbstractSubscribeHandler 子类
     */
    protected abstract AbstractSubscribeHandler getSubscribeHandler();

    /**
     * 微信取关处理类
     * @return AbstractUnsubscribeHandler 子类
     */
    protected abstract AbstractUnsubscribeHandler getUnsubscribeHandler();

    /**
     * 微信消息处理类
     * @return AbstractMsgHandler 子类
     */
    protected abstract AbstractMsgHandler getMsgHandler();

    /**
     * 微信二维码扫描处理类
     * @return AbstractScanHandler 子类
     */
    protected abstract AbstractScanHandler getScanHandler();

    @PostConstruct
    public void init() {
        final WxOpenInRedisConfigStorage configStorage = new WxOpenInRedisConfigStorage(jedisPool);
        configStorage.setComponentAppId(this.getServerConfig().getComponentAppId());
        configStorage.setComponentAppSecret(this.getServerConfig().getComponentSecret());
        configStorage.setComponentToken(this.getServerConfig().getComponentToken());
        configStorage.setComponentAesKey(this.getServerConfig().getComponentAesKey());
        super.setWxOpenConfigStorage(configStorage);
        wxOpenMessageRouter = new WxOpenMessageRouter(this);

        //点击菜单点击事件推送
        wxOpenMessageRouter.rule().msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.CLICK).handler(this.getMenuHandler()).end();

        //关注事件
        wxOpenMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE).handler(this.getSubscribeHandler()).end();

        //取消关注事件
        wxOpenMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.UNSUBSCRIBE).handler(this.getUnsubscribeHandler()).end();

        //扫码事件
        wxOpenMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SCAN).handler(this.getScanHandler()).end();

        //默认消息事件
        wxOpenMessageRouter.rule().async(false).handler(this.getMsgHandler()).end();
        /*wxOpenMessageRouter.rule().handler((wxMpXmlMessage, map, wxMpService, wxSessionManager) -> {
            logger.info("\n接收到 {} 公众号请求消息，内容：{}", wxMpService.getWxMpConfigStorage().getAppId(), wxMpXmlMessage);
            return null;
        }).next();*/
    }

    public WxOpenMessageRouter getWxOpenMessageRouter(){
        return wxOpenMessageRouter;
    }
}